#include "PointLight.h"

#define  _USE_MATH_DEFINES
#include <math.h>

using namespace DirectX;

PointLight::PointLight(DirectX::XMFLOAT4 ambientColor,
					   DirectX::XMFLOAT4 diffuseColor,
					   DirectX::XMFLOAT3 position,
					   DirectX::XMFLOAT3 lookat,
					   float near,
					   float far) : m_ambientColor(ambientColor),
					   m_diffuseColor(diffuseColor),
					   m_position(position),
					   m_lookAt(lookat),
					   m_near(near),
					   m_far(far)
{
	GenerateViewMatrix();
	GenerateProjectionMatrix();
}

PointLight::PointLight( const PointLight& )
{
}

PointLight::~PointLight()
{
}

const XMVECTOR PointLight::M_UP = XMVectorSet(0.f, 1.f, 0.f, 1.f);

void PointLight::SetAmbientColor( float r, float g, float b, float a )
{
	m_ambientColor = XMFLOAT4(r, g, b, a);
}

void PointLight::SetDiffuseColor( float r, float g, float b, float a )
{
	m_diffuseColor = XMFLOAT4(r, g, b, a);
}

void PointLight::SetPosition( float x, float y, float z )
{
	m_position = XMFLOAT3(x, y, z);
	GenerateViewMatrix();
}

void PointLight::SetLookAt( float x, float y, float z )
{
	m_lookAt = XMFLOAT3(x, y, z);
	GenerateViewMatrix();
}

DirectX::XMFLOAT4 PointLight::GetAmbientColor( void ) const
{
	return m_ambientColor;
}

DirectX::XMFLOAT4 PointLight::GetDiffuseColor( void ) const
{
	return m_diffuseColor;
}

DirectX::XMFLOAT3 PointLight::GetPosition( void ) const
{
	return m_position;
}

void PointLight::GenerateViewMatrix( void )
{
	m_viewMatrix = XMMatrixLookAtLH(XMVectorSet(m_position.x, m_position.y, m_position.z, 1.f),
		XMVectorSet(m_lookAt.x, m_lookAt.y, m_lookAt.z, 1.f),
		M_UP);
}

void PointLight::GenerateProjectionMatrix( void )
{
	float fieldOfView = M_PI / 2.f, aspectRatio = 1.f;
	m_projectionMatrix =  XMMatrixPerspectiveFovLH(fieldOfView, aspectRatio, m_near, m_far);
}

XMMATRIX PointLight::GetViewMatrix( void ) const
{
	return m_viewMatrix;
}

XMMATRIX PointLight::GetProjectionMatrix( void ) const
{
	return m_projectionMatrix;
}

void PointLight::SetProjection( float near, float far )
{
	m_near = near;
	m_far = far;
	GetProjectionMatrix();
}

void PointLight::SetNear( float near )
{
	m_near = near;
	GenerateProjectionMatrix();
}

void PointLight::SetFar( float far )
{
	m_far = far;
	GenerateProjectionMatrix();
}